<template>
	<a-card :bordered="false" class="mb-3">
		<div class="flex justify-between items-start">
			<a-form
				ref="searchFormRef"
				name="advanced_search"
				:model="searchFormState"
				class="ant-advanced-search-form"
				style="width: calc(100% - 100px)"
			>
				<a-space :size="16">
					<a-form-item label="搜索" name="searchKey" class="mb-0">
						<a-input
							v-model:value="searchFormState.searchKey"
							placeholder="请输入客户来源搜索"
							@pressEnter="table.refresh(true)"
						/>
					</a-form-item>

					<a-button type="primary" @click="table.refresh(true)">查询</a-button>
					<a-button @click="reset">重置</a-button>
					<!--					<a @click="toggleAdvanced" style="margin-left: 8px">-->
					<!--						{{ advanced ? '收起' : '展开' }}-->
					<!--						<component :is="advanced ? 'up-outlined' : 'down-outlined'" />-->
					<!--					</a>-->
				</a-space>
			</a-form>
			<a-button type="primary" @click="formRef.onOpen()">
				<template #icon><plus-outlined /></template>
				新增
			</a-button>
		</div>
	</a-card>

	<a-card :bordered="false">
		<!-- :row-selection="options.rowSelection" -->
		<s-table
			ref="table"
			:columns="columns"
			:data="loadData"
			:alert="options.alert.show"
			bordered
			:row-key="(record) => record.id"
			:tool-config="toolConfig"
		>
			<template #operator class="table-operator">
				<a-space>
					<!-- v-if="hasPerm('customerOrderAdd')" -->
					<!--					<a-button type="primary" @click="formRef.onOpen()">-->
					<!--						<template #icon><plus-outlined /></template>-->
					<!--						新增-->
					<!--					</a-button>-->
					<!-- v-if="hasPerm('customerOrderBatchDelete')" -->
					<!--					<xn-batch-delete :selectedRowKeys="selectedRowKeys" @batchDelete="deleteBatch" />-->
				</a-space>
			</template>
			<template #bodyCell="{ column, record }">
				<template v-if="column.dataIndex === 'useFlag'">
					<a-switch
						v-model:checked="record.useFlag"
						:checkedValue="CommonSwitchEnum.TRUE"
						:unCheckedValue="CommonSwitchEnum.FALSE"
						:disabled="record.builtIn === '1'"
						@change="(value) => onUseFlagClick(value, record)"
					/>
				</template>
				<template v-if="column.dataIndex === 'action'">
					<a-space>
						<!-- v-if="hasPerm('customerOrderEdit')" -->
						<!--						<a @click="formRef.onOpen(record)">编辑</a>-->
						<!-- v-if="hasPerm(['customerOrderEdit', 'customerOrderDelete'], 'and')" -->
						<!--						<a-divider type="vertical" />-->
						<!-- v-if="hasPerm('customerOrderDelete')" -->
						<a-button type="link" danger size="small" :disabled="record.builtIn === '1'" @click="deleteOne(record)"
							>删除</a-button
						>
					</a-space>
				</template>
			</template>
		</s-table>
	</a-card>

	<Form ref="formRef" @successful="table.refresh()" />
</template>

<script setup name="CustomerSource">
	import customerSourceApi from '@/api/biz/crm/customerSourceApi'
	import Form from './form.vue'
	import { Modal } from 'ant-design-vue'
	import { createVNode } from 'vue'
	import { ExclamationCircleOutlined } from '@ant-design/icons-vue'
	import { CommonSwitchEnum } from '@/utils/enum'

	const labelCol = { style: { width: '100px' } }

	let searchFormState = reactive({
		searchKey: null
	})
	const searchFormRef = ref()
	const table = ref()
	const formRef = ref()
	const toolConfig = { refresh: true, height: true, columnSetting: true, striped: false }
	// 查询区域显示更多控制
	const advanced = ref(false)
	const toggleAdvanced = () => {
		advanced.value = !advanced.value
	}
	const columns = [
		{
			title: '客户来源名称',
			dataIndex: 'name'
		},
		{
			title: '是否启用',
			dataIndex: 'useFlag'
		},
		{
			title: '创建时间',
			dataIndex: 'createAt'
		}
	]
	// 操作栏通过权限判断是否显示
	// if (hasPerm(['customerOrderEdit', 'customerOrderDelete'])) {
	columns.push({
		title: '操作',
		dataIndex: 'action',
		align: 'center',
		width: '100px'
	})
	// }
	const selectedRowKeys = ref([])
	// 列表选择配置
	const options = {
		// columns数字类型字段加入 needTotal: true 可以勾选自动算账
		alert: {
			show: false,
			clear: () => {
				selectedRowKeys.value = ref([])
			}
		},
		rowSelection: {
			onChange: (selectedRowKey, selectedRows) => {
				selectedRowKeys.value = selectedRowKey
			}
		}
	}
	const loadData = (parameter) => {
		const searchFormParam = JSON.parse(JSON.stringify(searchFormState))
		return customerSourceApi.page(Object.assign(parameter, searchFormParam)).then((data) => {
			return data
		})
	}
	// 重置
	const reset = () => {
		searchFormRef.value.resetFields()
		table.value.refresh(true)
	}
	// 切换开启状态
	const onUseFlagClick = (value, record) => {
		Modal.confirm({
			title: '提示',
			icon: createVNode(ExclamationCircleOutlined),
			content: `此操作将${value === CommonSwitchEnum.TRUE ? '启用' : '停用'}，是否继续？`,
			okText: '确定',
			okType: 'warning',
			cancelText: '取消',
			onOk() {
				console.log('OK')
				customerSourceApi.submitForm({ ...record, useFlag: value }, true).then(() => {
					table.value.refresh(true)
				})
			},
			onCancel() {
				console.log('Cancel')
				record.useFlag = value === CommonSwitchEnum.TRUE ? CommonSwitchEnum.FALSE : CommonSwitchEnum.TRUE
			}
		})
	}
	// 删除
	const deleteOne = (record) => {
		Modal.confirm({
			title: '提示',
			icon: createVNode(ExclamationCircleOutlined),
			content: '确定要删除吗？',
			okText: '确认',
			cancelText: '取消',
			onOk: () => {
				let params = [
					{
						id: record.id
					}
				]
				customerSourceApi.delete(params).then(() => {
					table.value.refresh(true)
				})
			}
		})
	}
	// 批量删除
	const deleteBatch = (params) => {
		customerSourceApi.delete(params).then(() => {
			table.value.clearRefreshSelected()
		})
	}
</script>
